<template>
 <div class="app-container">
    模糊查询条件
    <!--查询表单-->
    <el-card class="operate-container" shadow="never">
      <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="题目">
        <el-input v-model="reportName" placeholder="题目" /> 
      </el-form-item>

      <el-form-item label="更新时间">
        <el-date-picker
      v-model="value2"
      value-format="yyyy-MM-dd HH:mm:ss"
      type="datetimerange"
      :picker-options="pickerOptions"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      align="right">
    </el-date-picker>
    <el-form-item label="内容">
        <el-input v-model="report" placeholder="内容" maxlength="20" show-word-limit /> 
    </el-form-item>      
    </el-form-item>
        
        <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    </el-card>

     <!-- 工具按钮 -->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button class="btn-add" @click="batchRemove()" >批量删除</el-button>
    </el-card>

     <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      @selection-change="handleSelectionChange">
      <el-table-column type="selection"/>
      <el-table-column label="报告名称" prop="reportName" width="200"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180" />
      <el-table-column prop="report" label="报告内容" width="550" />
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="removeById(scope.row.id)">删除</el-button>
          <!-- <el-button type="text" size="mini" @click="reviseId(scope.row.id)">修改</el-button> -->

          <router-link :to="'/summary/index/'+scope.row.id">
            <el-button type="text" size="mini"> 修改</el-button>
          </router-link>        
        </template>
      </el-table-column>
    </el-table>


    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
 </div>

</template>

<script>
import indexApi from '@/api/summary/index.js'
import indexApi1 from '@/api/question/index.js'
  export default {
    data() {
      return {
        list: [], // 讲师列表
        total: 0, // 总记录数
        page: 1, // 页码
        limit: 5, // 每页记录数
        multipleSelection: [],// 批量删除选中的记录列表
        reportName: '',
        report:  '',
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        value2: ''
        }
        
    },
     created() { // 在渲染之前执行

  
    },
    mounted() { // 在渲染之后执行   

       
    },
    methods: {
      
      fetchData() {
        const summaary = {
            reportName: this.reportName,
            report: this.report,
            email:document.cookie.split("vue_admin_template_token=")[1].split(";")[0],
            mintime : this.value2[0],
            maxtime : this.value2[1]
          }
          indexApi.getpagelist(summaary,this.page,this.limit).then(response => {
            this.list = response.data.records
            this.total = response.data.total
          })
        
      },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    


    //根据id删除该题
    removeById(id) {
      this.$confirm('此操作将永久删除该题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return indexApi.removeById(id)
      }).then((response) => {
        this.fetchData()
        this.$message.success(response.message)
      })
    },
    
    handleSelectionChange(selection){
      this.multipleSelection=selection
    },
    // 重置表单
    resetData() {
      this.reportName='',
      this.report='',
      this.value2=''
    },



    batchRemove(){
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择要删除的记录！')
        return
      }
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点击确定，远程调用ajax
        // 遍历selection，将id取出放入id列表
        var idList = []
        this.multipleSelection.forEach(item => {
          idList.push(item.id)
          console.log(idList)
        })
        // 调用api
        return indexApi.Batchdeleter(idList)
      }).then((response) => {
        this.fetchData()
        this.$message.success(response.message)
      }).catch(error => {
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },



    }
  };
</script>